<?php
/*
send 
Installation:

Run the following query in your database:
create table quotes (id int default null, deleted tinyint(1) default 0, author varchar(50) default "", quote text, channel varchar(100) default "", time int, key(id));
*/

class doka extends module {

	public $title = "Ads module with ini";
	public $author = "hery";
	public $version = "0.9";

	//Methods here:

	public function init()
	{
		$this->timerClass->addTimer("doka_show", $this, "show", "", 60*10, false);
		$this->timerClass->addTimer("update_ini", $this, "update_ini", "", 60*5, false);
		$this->doka_ini = new ini("data/doka.ini");
		$this->channel_ini = new ini("data/channel.ini");

	}

	public function destroy()
	{
		$this->update_ini();
		$this->timerClass->removeTimer("update_ini");
		$this->timerClass->removeTimer("doka_show");
	}

	public function update_ini()
	{

		$this->doka_ini->writeIni();

		return true;
	}
	
	public function show()
	{
		$chans = $this->channel_ini->getVars("channels");

		foreach ($chans as $chan => $active)
		{
			if ($active == 1)
			{
				if ($var = $this->doka_ini->randomVar($chan))
				{
					$this->ircClass->privMsg($chan, $this->doka_ini->getIniVal($chan, $var));
				}
			}
		}
		
		return true;
	}
	
	public function admin($line, $args)
	{

		if ($args['nargs'] == 0)
		{
			$chan = "#serasera";
		}
		else
		{
			if (substr($args['arg1'], 0, 1) != "#")
			{
				$chan = "#serasera";
				$cmd = $args['arg1'];
				if (isset($args['arg2']))
				{
					$valiny = trim(substr($args['query'], (strpos($args['query'], $args['arg1'])) + (strlen($args['arg1']))));
				}
			}
			else
			{
				$chan = $args['arg1'];
				$cmd = isset($args['arg2'])?$args['arg2']: '';
				if (isset($args['arg3']))
				{
					$valiny = trim(substr($args['query'], (strpos($args['query'], $args['arg2'])) + (strlen($args['arg2']))));
				}
			}
		}
	
		if ($line['fromNick'] != "hery" && !$this->ircClass->isMode($line['fromNick'], $chan, 'o'))
		{
			$msg = "Tsy afaka mampiasa an'io baiko io ianao fa tsy op eo amin'ny " . $chan;
			$this->ircClass->privMsg($line['fromNick'], $msg);
			return;
		}
		switch ($cmd)
		{
			case "add";
				$this->doka_add($line, $chan, $cmd, $valiny);
			break;
			case "del";
				$this->doka_del($line, $chan, $cmd, $valiny);
			break;
			default:
				$this->doka_default($line, $chan, $cmd, $valiny);
			;
		}

	}
	
	public function doka_default($line, $chan, $cmd, $valiny)
	{
		if($msg = $this->doka_ini->getVars($chan))
		{
			$this->ircClass->privMsg($line['fromNick'], "Ireto ny filazana ao amin'ny " . $chan . ": ");
			foreach ($msg as $key => $val)
			{
				if ($key && $val)
				{
				$this->ircClass->privMsg($line['fromNick'], BOLD . $key . BOLD . ": " . $val);
				}
			}
		}
		else
		{
			$this->ircClass->notice($line['fromNick'], "Tsy misy filazana");
		}
	
	}
	
	public function doka_add($line, $chan, $cmd, $valiny)
	{
		if (!isset($valiny))
		{
			$this->ircClass->notice($line['fromNick'], "Tsy nety ny fampidirana. Soraty hoe " . BOLD . "!doka $chan add " . BOLD . "<filazana ampidirina>");
			return;
		}
		$vars = $this->doka_ini->getVars($chan);
		$this->doka_ini->setIniVal($chan, date('dHi'), $valiny);
		$this->ircClass->notice($line['fromNick'], "Tafiditra ny filazana");
	
	}

	public function doka_del($line, $chan, $cmd, $valiny)
	{
		if (!isset($valiny))
		{
			$this->ircClass->notice($line['fromNick'], "Tsy nety ny famafana. Soraty hoe " . BOLD . "!doka $chan del " . BOLD . "<laharan'ny filazana>. Raha tsy fantatrao ny laharana dia soraty hoe " . BOLD ."!doka $chan");
			return;
		}
		$id = intval($valiny);
		if ($id == 0)
		{
			$this->ircClass->notice($line['fromNick'], "Tsy nampiditra laharan'ny filazana hofafaina ianao. Soraty hoe " . BOLD . "!doka $chan del <id> " . BOLD . "ka ny <id> dia soloina ny laharan'ny filazana. Raha tsy fantatrao ny laharana dia soraty hoe " . BOLD . "!doka $chan");
			return;
		}
		$this->doka_ini->deleteVar($chan, $id);
		$this->ircClass->notice($line['fromNick'], "Voafafa ny filazana");

	}						
}

?>
